﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Chapter06.View
{
    /// <summary>
    /// CheckBoxListWindow.xaml 的交互逻辑
    /// </summary>
    public partial class CheckBoxListWindow : Window
    {
        public CheckBoxListWindow()
        {
            InitializeComponent();
        }

        private void ListBox_SelectionChanged(object sender, RoutedEventArgs e)
        {
            if (e.OriginalSource is CheckBox)
            {
                lst.SelectedItem = e.OriginalSource;
            }
            if (lst.SelectedItem == null)
                return;
            txtSelection.Text = String.Format("你选择的项目位置是 {0}.\r\n复选框状态是 {1}.", lst.SelectedIndex, ((CheckBox)lst.SelectedItem).IsChecked);
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            foreach (CheckBox item in lst.Items)
            {
                if (item.IsChecked == true)
                {
                    sb.Append(item.Content + " 已被选中.");
                    sb.Append("\r\n");
                }
            }
            txtSelection.Text = sb.ToString();
        }
    }
}
